<?php namespace App;

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission
{
    public $timestamps = false;
    protected $table = 'permissions';

    const STATUS_ON = 1;
    const STATUS_OFF = 2;

    static $StatusDisplay = [
        self::STATUS_ON => '开启',
        self::STATUS_OFF => '关闭'
    ];

    /**
     * 添加权限
     * @return array
     */
    public static function AddPermission()
    {
        $return = ['status' => 2, 'message' => '创建失败'];

        $permission = new Permission();
        $permission->name = request('name');
        $permission->display_name = request('display_name');
        $permission->orders = request('orders');
        $permission->pid = request('pid');
        if ($permission->save()) {
            $return = ['status' => 1, 'message' => '创建成功'];
        }

        return $return;
    }

    //修改权限
    public static function ModifyPermission($permission_id)
    {
        $return = ['status' => 2, 'message' => '修改失败'];

        $permission = Permission::findOrFail($permission_id);
        $permission->name = request('name');
        $permission->display_name = request('display_name');
        $permission->orders = request('orders');
        $permission->pid = request('pid');
        if(request('status')){
            $permission->status = request('status');
        }

        if ($permission->update()) {
            $return = ['status' => 1, 'message' => '修改成功'];
        }

        return $return;
    }
}